package org.bee.rest.id.server.startup;

import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.bee.rest.id.server.global.ResMgr;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;

public class StartupListener extends GuiceServletContextListener implements HttpSessionListener
{
	protected static final ResMgr _res = ResMgr.getInstance();

	public static Injector injector;
	
	@Override
	public void sessionCreated(HttpSessionEvent event) 
	{
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent event) 
	{
	}

	@Override
	public void contextDestroyed(ServletContextEvent event)
	{
	}

	@Override
	public void contextInitialized(ServletContextEvent event)
	{
		super.contextInitialized(event);
		
		ServletContext context = event.getServletContext();
		context.log(_res.getString("msg_starting_restws"));
		
		//initProperties();
		
		context.log(_res.getString("msg_started_restws"));
	}
	
	/**
	 * Initializes system properties by including environment settings
	 */
	protected void initProperties()
	{
		Map<String, String> env = System.getenv();
		Properties props = System.getProperties();
		props.putAll(env);
		System.setProperties(props);
	}

	@Override
	protected Injector getInjector() 
	{
		injector = Guice.createInjector(new ServiceModule(), new JerseyServletModule());
		return injector;
	}
}